import store from '@/store'
import { page as key } from './permission'
const utils = {
	dateFormat: function (date, fmt = 'YYYY-MM-DD HH:mm:ss') {
	  var o = {
		'M+': date.getMonth() + 1,
		'D+': date.getDate(),
		'h+': date.getHours() % 12 === 0 ? 12 : date.getHours() % 12,
		'H+': date.getHours(),
		'm+': date.getMinutes(),
		's+': date.getSeconds(),
		'q+': Math.floor((date.getMonth() + 3) / 3),
		'S': date.getMilliseconds()
	  }
	  var week = {
		'0': '\u65e5',
		'1': '\u4e00',
		'2': '\u4e8c',
		'3': '\u4e09',
		'4': '\u56db',
		'5': '\u4e94',
		'6': '\u516d'
	  }
	  if (/(Y+)/.test(fmt)) {
		fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
	  }
	  if (/(E+)/.test(fmt)) {
		fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? '\u661f\u671f' : '\u5468') : '') + week[date.getDay() + ''])
	  }
	  for (var k in o) {
		if (new RegExp('(' + k + ')').test(fmt)) {
		  fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
		}
	  }
	  return fmt
	},
	accAdd: function (arg1, arg2) { // +
		var r1, r2, m
		try { r1 = arg1.toString().split('.')[1].length } catch (e) { r1 = 0 }
		try { r2 = arg2.toString().split('.')[1].length } catch (e) { r2 = 0 }
		m = Math.pow(10, Math.max(r1, r2))
		return (Math.round(arg1 * m) + Math.round(arg2 * m)) / m		
	},
	accSub: function (arg1, arg2) { // -
		var r1, r2, m, n
		try { r1 = arg1.toString().split('.')[1].length } catch (e) { r1 = 0 }
		try { r2 = arg2.toString().split('.')[1].length } catch (e) { r2 = 0 }
		m = Math.pow(10, Math.max(r1, r2))
		n = (r1 >= r2) ? r1 : r2
		return ((arg1 * m - arg2 * m) / m).toFixed(n)		
	},
	accMul: function (arg1, arg2) { // *
		var m = 0; var s1 = arg1.toString(); var s2 = arg2.toString()
		try { m += s1.split('.')[1].length } catch (e) {}
		try { m += s2.split('.')[1].length } catch (e) {}
		return Number(s1.replace('.', '')) * Number(s2.replace('.', '')) / Math.pow(10, m)		
	},
	accEqu: function (arg1, arg2) { // /
		var t1 = 0, t2 = 0, r1, r2;
		try { t1 = arg1.toString().split(".")[1].length } catch (e) { }
		try { t2 = arg2.toString().split(".")[1].length } catch (e) { }
		r1 = Number(arg1.toString().replace(".", ""))
		r2 = Number(arg2.toString().replace(".", ""))
		return (r1 / r2) * Math.pow(10, t2 - t1)		
	},
	Fxied: function (num, n = 2) {
			if (n > 20 || n < 0) {
					throw new RangeError('toFixed() digits argument must be between 0 and 20');
			}
			const number = num;
			if (isNaN(number) || number >= Math.pow(10, 21)) {
					return number.toString();
			}
			if (typeof (n) == 'undefined' || n == 0) {
					return (Math.round(number)).toString();
			}

			let result = number.toString();
			const arr = result.split('.');

			// 整数的情况
			if (arr.length < 2) {
					result += '.';
					for (let i = 0; i < n; i += 1) {
							result += '0';
					}
					return result;
			}

			const integer = arr[0];
			const decimal = arr[1];
			if (decimal.length == n) {
					return result;
			}
			if (decimal.length < n) {
					for (let i = 0; i < n - decimal.length; i += 1) {
							result += '0';
					}
					return result;
			}
			result = integer + '.' + decimal.substr(0, n);
			const last = decimal.substr(n, 1);

			// 四舍五入，转换为整数再处理，避免浮点数精度的损失
			if (parseInt(last, 10) >= 5) {
					const x = Math.pow(10, n);
					result = (Math.round((parseFloat(result) * x)) + 1) / x;
					result = result.toFixed(n);
			}

			return result;
	},
	Login: () => {
		const user = store.state.user.userInfo
		if (!user) {
			uni.navigateTo({
				url: '/pages/login/index'
			})
			return false
		}
		return true
	},
	LinkTo: function(item) {
		// #ifndef MP-WEIXIN
		if (item) {
			if (item.link !== true) {
				if (item.link.indexOf('个人中心') !== -1 || key.includes(item.link)) {
					const flag = this.Login()
					if (!flag) return
				}				
			} else {
				this.Login()
				return				
			}
		} else {
			this.Login()
			return
		}
		// #endif
		switch (item.link) {
			case '拼团页面':
				uni.navigateTo({
					url: '/usercenter/assemble/assemble',
				})
				break;
			case '抢购页面':
				uni.navigateTo({
					url: '/packageB/pages/Buy-limit',
				})
				break;
			case '礼包列表':
				uni.navigateTo({
					url: '/pages/spree/packagelist?tag=6',
				})
				break;
			case '签到积分':
				uni.navigateTo({
					url: '/assets/vipcard',
				})
				break;
			case '购物币':
				uni.navigateTo({
					url: '/assets/coin',
				})
				break;
			case '优惠券':
				uni.navigateTo({
					url: '/assets/mycoupon',
				})
				break;
			case '会员卡':				
				uni.navigateTo({
					url: '/assets/vipcard',
				})
				break;
			case '续费专区':
				uni.navigateTo({
					url: '/packageA/pages/renew',
				})
				break;
			case '视频专区':
				uni.navigateTo({
					url: '/packageB/pages/videoList/index',
				})
				break;
			case '阅读专区':
				uni.navigateTo({
					url: '/shop/article?title='+item.title,
				})
				break;
			case '个人中心：我的订单':
				if (store.state.model.shop.type == 5 || store.state.model.shop.type == 1) {
					uni.navigateTo({
						url: '/pages/orders/list'
					})
				} else if (store.state.shop.type == 2) {
					uni.navigateTo({
						url: '/packageB/pages/order2'
					})
				} else if (store.state.shop.type == 4) {
					uni.navigateTo({
						url: '/packageB/pages/order4',
					})
				}
				break;
			case '个人中心：我的足迹':
				uni.navigateTo({
					url: '/usercenter/footprint'
				})
				break;
			case '个人中心：我的收藏':
				uni.navigateTo({
					url: "/usercenter/favorite"
				})
				break;
			case '个人中心：收货地址':
				uni.navigateTo({
					url: '/usercenter/address'
				})
				break;
			case '个人中心：我的资产':
				uni.navigateTo({
					url: '/assets/index'
				})
				break;
			case '个人中心：编辑资料':
				uni.navigateTo({
					url: '/usercenter/editdata'
				})
				break;
			case '个人中心：在线客服':
				uni.navigateTo({
					url: '/packageB/pages/online-service'
				})
				break;
			case '个人中心：邀请小伙伴':
				uni.navigateTo({
					url: '/packageB/pages/share-app'
				})
				break;
			case '个人中心：我的赠送':
				uni.navigateTo({
					url: '/pages/my-give'
				})
				break;
			case '个人中心：我的特权':
				uni.navigateTo({
					url: '/usercenter/myprivilege'
				})
				break;
			case '个人中心：分销中心':
				uni.navigateTo({
					url: '/usercenter/distribution'
				})
				break;
			case '个人中心：拼团':
				uni.navigateTo({
					url: '/usercenter/assemble/assemble'
				})
				break;
		}
		if (item.type_id && store.state.model.shop.type === 5) {
			let title = item.title ? item.title : ''
			let url = '/pages/search/productlist?id=' + item.type_id
			if (title) {
				url = '/pages/search/productlist?id=' + item.type_id + '&title=' + title
			}
			uni.navigateTo({
				url: url
			})
		} else if (item.supplyId && store.state.model.shop.type === 1) {
			uni.navigateTo({
				url: '/shop/index?id=' + item.supplyId,
			})
		} else if (item.type_id && store.state.model.shop.type === 1) {
			uni.navigateTo({
				url: '/shop/shop_list?id=' + item.type_id + '&name=' + item.link,
			})
		} else if (item.pid && ( store.state.model.shop.type === 1 || store.state.model.shop.type === 5 )) {
			let url = ''
			if (store.state.model.shop.type === 1) {
				url = '/pages/orders/productinfo?pid=' + item.id + '&supply_id' + item.supply_id
			} else if (store.state.model.shop.type === 5) {
				url = '/pages/orders/productinfo?pid=' + item.pid
			}
			uni.navigateTo({
				url: url
			})
		} else if (item.supply_id || item.type_id && store.state.model.shop.type === 2) {
			uni.navigateTo({
				url: '/packageB/pages/shop2?supply_id=' + item.supply_id + '&type_id=' + item.type_id
			})
		}

	}	
}

export default utils